package com.letter.otools.day.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.letter.otools.day.NativeWebActivity;

/**
 * @Description: JS交互
 * @Author: dinghu
 * @CreateDate: 2023/3/23 9:46
 */
public class WebViewUtils {


    public static void goWebForResult(Activity context, int requestCode, String url) {
        goWebForResult(context, requestCode, url);
    }

    public static void goWebForResult(Activity context, int requestCode, String title, String url) {
        goWebForResult(context, requestCode, title, false, url);
    }

    public static void goWebForResult(Activity context, int requestCode, String title, boolean immersion, String url) {
        String rightMenus[] = {};
        goWebForResult(context, requestCode, title, immersion, url, null, null);
    }

    public static void goWebForResult(Activity context, int requestCode, String title, boolean immersion, String url, Bundle params, String rightMenus) {
        String urlFix = buildUrl(url, immersion);
        context.startActivityForResult(buildIntent(context, title, immersion, urlFix, params, rightMenus), requestCode);
    }

    public static void goWeb(Context context, String url) {
        goWeb(context, null, url);
    }

    public static void goWeb(Context context, String title, String url) {
        goWeb(context, title, false, url);
    }

    public static void goWeb(Context context, String title, boolean immersion, String url) {
        goWeb(context, title, immersion, url, null, null);
    }

    public static void goWeb(Context context, String title, boolean immersion, String url, Bundle params, String rightMenus) {
        String urlFix = buildUrl(url, immersion);
        context.startActivity(buildIntent(context, title, immersion, urlFix, params, rightMenus));
    }

    private static String buildUrl(String url, boolean immersion) {
        String h5Prefix = "";//BuildConfig.H5PREFIX;
        if (!url.startsWith("http://") && !url.startsWith("https://")) {
            if (url.startsWith("/")) {
                url = h5Prefix + "/#" + url;
            } else {
                url = h5Prefix + "/#/" + url;
            }
        }
        return url;
    }

    private static Intent buildIntent(Context context, String title, boolean immersion, String url, Bundle params, String... rightMenus) {
        Intent intent = new Intent(context, NativeWebActivity.class);
        if (!TextUtils.isEmpty(title)) {
            intent.putExtra("title", title);
        }
        if (params != null) {
            intent.putExtra("rightMenus", rightMenus);
        }
        intent.putExtra("immersion", immersion);
        intent.putExtra("url", url);
        if (params != null) {
            intent.putExtra("params", params);
        }

        return intent;
    }
}
